﻿#include "epiboly.h"
#include "ui_epiboly.h"

Epiboly::Epiboly(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Epiboly)
{
    ui->setupUi(this);
    QString date=QDate::currentDate().toString("yyyyMMdd-");
    ui->batch_pre->setText(date);
}

Epiboly::~Epiboly()
{
    delete ui;
}

bool Epiboly::init()
{
    if(option.load(LST_CONTRACTOR))
    {
        ui->contractor->addItems(option.getStringList());
        ui->contractor->addItem(OPT_EDIT);
        ui->contractor->setCurrentIndex(-1);

        model=new QSqlQueryModel(this);
        ui->tableView->setModel(model);
        ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
        return true;
    }
    else
        return false;
}

void Epiboly::on_btn_add_clicked()
{
    if(ui->batch_post->text().isEmpty() || ui->contractor->currentText().isEmpty() || ui->job->toPlainText().isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("后缀、承包商、作业不能为空！"),QMessageBox::Ok);
        return;
    }

    batch=ui->batch_pre->text() + ui->batch_post->text();

    if(ui->name->text().isEmpty() || ui->number->text().isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"),QMessageBox::Ok);
        return;
    }
    name=ui->name->text();
    number=ui->number->text();

    QSqlQuery query;
    query.prepare("insert into td_epiboly_stuff (batch,name,number) values (?,?,?)");
    query.addBindValue(batch);
    query.addBindValue(name);
    query.addBindValue(number);
    if(query.exec())
    {
        updateView();
        ui->name->clear();
        ui->number->clear();
    }
}

void Epiboly::updateView()
{
    QSqlQuery query(QString("select batch,name,number from td_epiboly_stuff where batch='%1'").arg(batch));
    model->setQuery(query);

    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("批次"));
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("名称"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("数量"));
}

void Epiboly::on_btn_submit_clicked()
{
    if(ui->batch_post->text().isEmpty() || ui->contractor->currentText().isEmpty() || ui->job->toPlainText().isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("后缀、承包商、作业不能为空！"),QMessageBox::Ok);
        return;
    }
    batch=ui->batch_pre->text() + ui->batch_post->text();
    contractor=ui->contractor->currentText();
    job=ui->job->toPlainText();
    remark=ui->remark->text();

    if(saveRecord())
    {
        ui->batch_post->clear();
        ui->contractor->setCurrentIndex(-1);
        ui->job->clear();
        ui->remark->clear();
        batch.clear();
        ui->lbl_status->setText(QStringLiteral("记录已提交"));
    }
    else
        ui->lbl_status->setText(QStringLiteral("提交失败！数据库异常"));
}

bool Epiboly::saveRecord()
{
    QSqlQuery query;
    query.prepare("insert into td_epiboly (date,batch,contractor,job,remark) values (?,?,?,?,?)");
    query.addBindValue(Env::date);
    query.addBindValue(batch);
    query.addBindValue(contractor);
    query.addBindValue(job);
    query.addBindValue(remark);
    return query.exec();
}

void Epiboly::reload(const OptionType &type)
{
    if(type == O_CONTRACTOR)
    {
        if(option.load(LST_CONTRACTOR))
        {
            ui->contractor->clear();
            ui->contractor->addItems(option.getStringList());
            ui->contractor->addItem(OPT_EDIT);
        }
    }
}

void Epiboly::on_contractor_currentTextChanged(const QString &text)
{
    if(text == OPT_EDIT)
    {
        OptionEditor editor;
        if(editor.detect(O_CONTRACTOR))
        {
            connect(&editor,SIGNAL(reload(OptionType)),this,SLOT(reload(OptionType)));
            if(editor.exec() == QDialog::Rejected)
                ui->contractor->setCurrentIndex(-1);
        }
        else
            QMessageBox::critical(this,QStringLiteral("错误"),QStringLiteral("无法加载配置文件！"),QMessageBox::Ok);
    }
}
